<?php

/**
 * IP及CURL辅助组件
 * 
 * @author Lonxon
 * @version 1.5 Core
 */
class Core_Plugin_Http {

    /**
     * 获取客户端IP地址
     */
    static public function ip() {
        if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {
            $ip = getenv("HTTP_CLIENT_IP");
        } else {
            if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {
                $ip = getenv("HTTP_X_FORWARDED_FOR");
            } else {
                if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {
                    $ip = getenv("REMOTE_ADDR");
                } else {
                    if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {
                        $ip = $_SERVER['REMOTE_ADDR'];
                    } else {
                        $ip = "unknown";
                    }
                }
            }
        }
        return ($ip);
    }

    /**
     * 发送 HTTP 请求,支持 GET POST JSON方式
     * 
     * @param string $url 请求链接
     * @param array $param 请求参数
     * @param string $method 请求方式
     * @return array/boolean
     */
    static public function request($url, $param = array(), $method = 'GET') {
        $url = (strstr($url, 'http') === false) ? ('http://' . $_SERVER["HTTP_HOST"] . $url) : $url;
        $data = false;
        switch ($method) {
            case 'JSON':
                $param = is_array($param) ? urldecode(json_encode($param)) : '';
                $curl = curl_init();
                curl_setopt($curl, CURLOPT_URL, $url);
                curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                    'Content-Type: application/json',
                    'Content-Length: ' . strlen($param)
                ));
                curl_setopt($curl, CURLOPT_POST, 1);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
                curl_setopt($curl, CURLOPT_TIMEOUT, 20);
                curl_setopt($curl, CURLOPT_HEADER, 0);
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
                $data = curl_exec($curl);
                if (curl_errno($curl)) {
                    $data = false;
                }
                curl_close($curl);
                break;
            case 'POST':
                $param = is_array($param) && $param ? $param : array();
                $curl = curl_init();
                curl_setopt($curl, CURLOPT_URL, $url);
                curl_setopt($curl, CURLOPT_POST, 1);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
                curl_setopt($curl, CURLOPT_TIMEOUT, 20);
                curl_setopt($curl, CURLOPT_HEADER, 0);
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
                $data = curl_exec($curl);
                if (curl_errno($curl)) {
                    $data = false;
                }
                curl_close($curl);
                break;
            case 'GET':
                $param = $param ? $param : '';
                if (is_array($param)) {
                    $tmp = array();
                    foreach ($param as $k => &$v) {
                        if (is_array($v))
                            $val = implode(',', $v);
                        $tmp[] = $k . '=' . urlencode($v);
                    }
                    $param = implode('&', $tmp);
                }
                $url = $url . ($param ? ('?' . $param) : '');
                $curl = curl_init();
                curl_setopt($curl, CURLOPT_URL, $url);
                curl_setopt($curl, CURLOPT_HTTPGET, 1);
                curl_setopt($curl, CURLOPT_TIMEOUT, 20);
                curl_setopt($curl, CURLOPT_HEADER, 0);
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
                $data = curl_exec($curl);
                if (curl_errno($curl)) {
                    $data = false;
                }
                curl_close($curl);
                break;
        }
        return $data ? json_decode($data, true) : false;
    }
}
